package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.Good;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.UserTake;
import com.net.tkc.pro.impl.GoodImpl;
import com.net.tkc.pro.impl.UserTakeImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/UserTake/")
public class UserTakeController {
    ResultEntity resultEntity = null;
    @Resource
    UserTakeImpl userTakeImpl;

    /**
     * 根据id查找聊天记录
     * @param userfrom 帖子id
     * @return
     */
    @GetMapping("UserFrom")
    private String getUserFrom(
            @RequestParam("userfrom") int userfrom
    ){
        resultEntity = new ResultEntity();
        try{
            List<UserTake> list=userTakeImpl.getUserFrom(userfrom);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 根据id查找聊天记录
     * @param userto 用户id2
     * @return
     */
    @GetMapping("UserTo")
    private String getUserTo(
            @RequestParam("userto") int userto
    ){
        resultEntity = new ResultEntity();
        try{
            List<UserTake> list=userTakeImpl.getUserTo(userto);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 撤回聊天记录
     * @param id iD
     * @return
     */
    @DeleteMapping("UserTake")
    private String delUserTake(@RequestParam("id") int id){
        resultEntity = new ResultEntity();
        try{
            int result = userTakeImpl.delUserTake(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 添加聊天记录
     * @param userfrom 聊天人1
     * @param userto 聊天人2
     * @param message 聊天内容
     * @return
     */
    @PostMapping("UserTake")
    private String addUserTake(
            @RequestParam("userfrom") int userfrom,
            @RequestParam("userto") int userto,
            @RequestParam("message") String message
    ){
        resultEntity = new ResultEntity();
        UserTake userTake=new UserTake();
        userTake.setUserfrom(userfrom);
        userTake.setUserto(userto);
        DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date=new Date();
        String Time=dateFormat.format(date);
        userTake.setDttime(Time);
        userTake.setMessage(message);
        try{
            int result = userTakeImpl.addUserTake(userTake);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 根据id查找聊天记录
     * @param userto 用户id2
     * @param userfrom 用户id2
     * @return
     */
    @GetMapping("UserTakeFrom")
    private String getUserTake(
            @RequestParam("userto") int userto,
            @RequestParam("userfrom") int userfrom
    ){
        resultEntity = new ResultEntity();
        try{
            List<UserTake> list=userTakeImpl.getUserTake(userto,userfrom);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
